package com.list88zhu.android.sunflower.jetpack;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;

import com.list88zhu.android.sunflower.R;
import com.list88zhu.android.sunflower.jetpack.room.AppDatabase;
import com.list88zhu.android.sunflower.jetpack.room.Plant;
import com.list88zhu.android.sunflower.widget.CalendarDialog;

import java.util.Date;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/8/26
 */
public class PlantAddActivity extends AppCompatActivity {

    private EditText titleView;
    private EditText contentView;
    private Button beginDateBtn;
    private Button endDateBtn;
    private EditText executorView;

    private AppDatabase mAppDatabase;
    private CalendarDialog calendarDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plant_add);
        titleView = findViewById(R.id.title);
        contentView = findViewById(R.id.content);
        beginDateBtn = findViewById(R.id.beginDate);
        endDateBtn = findViewById(R.id.endDate);
        executorView = findViewById(R.id.executor);

        beginDateBtn.setOnClickListener(view -> {
            calendarDialog.show(getSupportFragmentManager());
            calendarDialog.setCallback((dateString, date) -> {
                beginDateBtn.setTag(date);
                beginDateBtn.setText(dateString);
            });
        });
        endDateBtn.setOnClickListener(view -> {
            calendarDialog.setCallback((dateString, date) -> {
                endDateBtn.setTag(date);
                endDateBtn.setText(dateString);
            });
            calendarDialog.show(getSupportFragmentManager());
        });

        findViewById(R.id.submitBtn).setOnClickListener(view -> {

            Plant plant = new Plant();
            plant.setTitle(titleView.getText().toString().trim());
            plant.setContent(contentView.getText().toString().trim());
            plant.setBeginDate((Date)beginDateBtn.getTag());
            plant.setEndDate((Date)endDateBtn.getTag());
            plant.setExecutor(executorView.getText().toString().trim());
            mAppDatabase.getPlantDao().insertPlant(plant);
        });


        mAppDatabase = AppDatabase.getDefault(this);
        calendarDialog = new CalendarDialog();

    }
}
